var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var getHtmlConfig = function(name){
    return {
        template    : './src/view/' + name + '.html',
        filename    : './src/view/' + name + '.html',
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    };
};

config = { 
    entry: {
        'common'        : './src/page/common/index.js',
        'index'         : './src/page/index/index.js',
        'user-login'    : './src/page/user-login/index.js',
        'search'        : './src/page/search/index.js',
        'search'        : './src/page/search/index.js',
        'detail'        : './src/page/detail/index.js',
        'user-register' : './src/page/user-register/index.js',
        'cart'          : './src/page/cart/index.js',
        'checkout'      : './src/page/checkout/index.js',
        'list'          : './src/page/list/index.js',
        'pay_success'   : './src/page/pay_success/index.js',
    },
    output: { 
        path        : './dist',
        filename    : 'js/[name].js'
    }, 
    // devServer: {
        
    //     proxy: {
    //       '/': {
    //         target: 'http://49.234.18.233:8081/', //API服务器的地址
    //         changeOrigin: true, // 是否跨域，虚拟的站点需要更管origin
    //         // pathRewrite: {
    //         //     '^/api': '',
    //         // }
    //       },
    //     },
    // },

    externals : {
        'jquery' : 'window.$'
    },
    module: {
        loaders: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader","css-loader") },
            { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader'},
            { test: /\.string$/, loader: 'html-loader'}
        ]
    },
    resolve : {
        alias : {
            root            : __dirname + '/src',
            node_modules    : __dirname + '/node_modules',
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            images          : __dirname + '/src/images',
        }
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name : 'common',
            filename : 'js/base.js'
        }),
        new ExtractTextPlugin("css/[name].css"),
        new HtmlWebpackPlugin(getHtmlConfig('index')),
        new HtmlWebpackPlugin(getHtmlConfig('user-login')),
        new HtmlWebpackPlugin(getHtmlConfig('user-register')),
        new HtmlWebpackPlugin(getHtmlConfig('result')),
        new HtmlWebpackPlugin(getHtmlConfig('search')),
        new HtmlWebpackPlugin(getHtmlConfig('detail')),
        new HtmlWebpackPlugin(getHtmlConfig('cart')),
        new HtmlWebpackPlugin(getHtmlConfig('checkout')),
        new HtmlWebpackPlugin(getHtmlConfig('list')),
        new HtmlWebpackPlugin(getHtmlConfig('pay_success')),
    ],
}

module.exports = config